package com.audi.view;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.activity.NewActivityPlaceInfo;
import com.audi.activity.PlaceCardDetailActivity;
import com.audi.activity.SelectDevicesActivity;
import com.audi.activity.StatusDevicesActivity;
import com.audi.dto.SceneItem;
import com.audi.message.SendMessage;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class BaseSceneDialog {
	private Dialog mDialog;
	private Context context;
	private BaseActivity mBaseActivity;
	private Button btnCancel, btnOk;
	private EditText renameEditText;
	private SendMessage sendMessage;
	private AlertDialog.Builder mRemoveSceneDialog;

	public BaseSceneDialog(Context activity) {
		this.context = activity;
		sendMessage = new SendMessage();
		this.mBaseActivity = (BaseActivity) activity;
		this.mDialog = new Dialog(context);

	}

	/**
	 * author: HoangNT10 Method init for rename scene
	 * 
	 * @param btnCancelText
	 * @param btnOkText
	 */
	private void initDialogInput(String btnCancelText, String btnOkText) {
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialog.setContentView(R.layout.rename_device_dialog);

		btnCancel = (Button) mDialog
				.findViewById(R.id.btn_rename_device_dialog_skip);
		btnOk = (Button) mDialog.findViewById(R.id.btn_rename_device_dialog_ok);
		renameEditText = (EditText) mDialog
				.findViewById(R.id.edt_rename_device_dilog_input);
		renameEditText.setHint("Rename scene");
		btnCancel.setText(btnCancelText);
		btnOk.setText(btnOkText);

		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mDialog.dismiss();
			}
		});
	}

	/**author: HoangNT10
	 * Method rename for scene.
	 * @param scene
	 * @param callBack
	 */
	public void editNameSence(final SceneItem scene, final Handler callBack) {
		final SceneItem sceneItem = scene;
		initDialogInput("Cancel", "Ok");
		btnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (renameEditText != null
						&& renameEditText.getText().toString().trim().length() > 0) {
					sceneItem
							.setSceneTitle(renameEditText.getText().toString());
					sendMessage.updateScene(scene, callBack);
				}
				mDialog.dismiss();
			}
		});
		mDialog.show();
	}

	/**
	 * author: HoangNT10 
	 * Method remove scene
	 * 
	 * @param sceneItem
	 * @param callBackScene
	 */
	public void onRemoveSceneDialog(final SceneItem sceneItem,
			final Handler callBackScene) {
		mRemoveSceneDialog = new AlertDialog.Builder(context);
		mRemoveSceneDialog.setTitle("Remove this scene");
		mRemoveSceneDialog.setMessage("Do you really want to remove "
				+ sceneItem.getSceneTitle());
		mRemoveSceneDialog
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								sendMessage.deleteScene(sceneItem,
										callBackScene);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).show();
	}
	/**author: HoangNT10
	 * Method add a scene
	 * @param sceneItem
	 * @param callBackScene
	 */
	public void onAddSceneDialog() {
		final AudiDialog dialogNewSceneName = new AudiDialog(context);
		dialogNewSceneName.setLayout(R.layout.new_scene_name_dialog);
		dialogNewSceneName.show();

		@SuppressWarnings("unused")
		final EditText mEdtNewScene = (EditText) dialogNewSceneName
				.findViewById(R.id.edt_new_scene);

		Button mBtnNewSceneCancel = (Button) dialogNewSceneName
				.findViewById(R.id.btn_new_scene_cancel);
		final Button mBtnNewSceneOk = (Button) dialogNewSceneName
				.findViewById(R.id.btn_new_scene_ok);

		mBtnNewSceneCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialogNewSceneName.dismiss();

			}
		});
		mBtnNewSceneOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SceneItem sceneItem =new SceneItem();
				sceneItem.setSceneTitle(mEdtNewScene.getText().toString());
				//Check is exist name scane.
				/*if (sendMessage.checkExistScene(SceneItem,))*/
				Intent intentDeviceCard = new Intent(context,
						StatusDevicesActivity.class);
				/*Bundle bundleInfoPlace = new Bundle();
				bundleInfoPlace.putSerializable("sceneItem",
						sceneItem);
				intentDeviceCard.putExtras(bundleInfoPlace);
				context.startActivity(intentDeviceCard);*/
				dialogNewSceneName.dismiss();
			}
		});
	}
	
}
